package com.weipai.springboot.module.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.weipai.springboot.model.AdminLoginRecord;
import com.weipai.springboot.model.Customer;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.AdminService;
import com.weipai.springboot.module.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author songmengqiang
 * @create 2018-08-29 18:47
 **/

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;

    //登录
    @RequestMapping(value = "/user/login")
    //@RequestParam(value = "code", required = false), String code
    public Response login(String username, String password , HttpServletRequest request, HttpSession session) {
        String code = request.getParameter("code");
        System.out.print("-------------------------------"+code);
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
//        Subject subject = SecurityUtils.getSubject();
//        subject.login(usernamePasswordToken);
        Response login = userService.login(username, password, code);
        //更新用户登录时间，也可以在ShiroRealm里面做
//        session.setAttribute("loginuser", login.getData());
        String id = session.getId();
     /*   login.setSessionId(id);
        MyAuthenticationFilter.sessionid=id;*/
//        Session session1 = subject.getSession();
//        session1.setAttribute(User.KEY, login.getData());

        return login;
    }


    /*  第三方登录 -- 微信
        String wxUid;               微信登录id
        String customerLoginName;   用户登录名
        String customerPassword;    密码
     */
    @RequestMapping(value = "/user/thirdPartyLogin")
    public Response thirdPartyLogin(Customer customer) {
        return userService.thirdPartyLogin(customer);
    }


    //退出
    @RequestMapping(value = "/user/loginOut", method = RequestMethod.GET)
    public Response loginOut() {
        return userService.loginOut();
    }


    @RequestMapping(value = "/user/test", method = RequestMethod.GET)
    public String test(@RequestParam("name") String name) {
        return "hello " + name;
    }


    @RequestMapping(value = "/user/error", method = RequestMethod.GET)
    public String error() {
        return "system error";
    }


    /**
     * 管理员重置密码
     *
     * @param oldPassword：旧密码
     * @param newPassword：新密码
     * @param twoPassword：新密码确认
     * @return
     */
    @RequestMapping(value = "/user/resetAdminPassword", method = RequestMethod.GET)
    public Response resetAdminPassword(@RequestParam(value = "oldPassword", required = false) String oldPassword,
                                       @RequestParam(value = "adminId", required = false) String adminId,
                                       @RequestParam(value = "newPassword", required = false) String newPassword,
                                       @RequestParam(value = "twoPassword", required = false) String twoPassword) {
        return userService.resetAdminPassword(adminId, oldPassword, newPassword, twoPassword);
    }

    /**
     * 记录管理员登录日志
     *
     * @param adminLoginRecord
     * @return
     */
    @RequestMapping(value = "/user/saveAdminLoginRecord", method = RequestMethod.POST)
    public Response saveAdminLoginRecord(AdminLoginRecord adminLoginRecord) {
        return adminService.saveAdminLoginRecord(adminLoginRecord);
    }

    /**
     * 验证用户
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "/user/verifyCustomer", method = RequestMethod.GET)
    public Response verifyCustomer(String username) {
        return userService.verifyCustomer(username);
    }


}
